
<template>
  <el-form ref="postForm" :model="postForm" :rules="rules">
    <sticky :class-name="'sub-navbar'">
      <el-button v-if="!isEdit" @click="showGuide">显示帮助</el-button>
      <el-button
        v-loading="loading"
        type="success"
        style="margin-left: 10px"
        @click="submitForm"
      >
        修改密码
      </el-button>
    </sticky>
    <div class="detail-container">
      <el-row>
        <el-col :span="24">
              <el-form-item prop="username" label="用户名：" :label-width="labelWidth" >
                <el-input
                  v-model="postForm.username"
                  placeholder="用户名"
                  disabled
                />
              </el-form-item>          
              <el-form-item prop="password" label="密码：" :label-width="labelWidth" v-show="!isEdit">
                <el-input
                  v-model="postForm.password"
                  placeholder="密码"
                  show-password
                  :disabled="isEdit"
                />
              </el-form-item>
              <el-form-item prop="password1" label="确认密码：" :label-width="labelWidth" v-show="!isEdit">
                <el-input
                  v-model="password1"
                  placeholder="确认密码"
                  show-password
                  :disabled="isEdit"
                />
              </el-form-item>
        </el-col>
      </el-row>
    </div>
  </el-form>
</template>

<script>
// eslint-disable-next-line
/* eslint-disable */
import Cookies from 'js-cookie'
import { mapGetters } from 'vuex'
import Sticky from '../../components/Sticky'
import {updatePasswordUser, getOneUser ,listUser } from '../../api/user'
import MarkdownEditor from '@/components/MarkdownEditor'

export default {
  components: { Sticky, MarkdownEditor },
  props: {
    isEdit: Boolean
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error(fields[rule.field] + '必须填写'))
      } else {
        callback()
      }
    }
    
    const validatePassword = (rule, value, callback) => {
     
          //console.log(value)
        if (value === '') {
          callback(new Error('密码不能为空'))
        } else {
          callback()
        }
      
    }
    const validatePassword1 = (rule, value, callback) => {
      //console.log(this.password1)
       if (this.password1 === '') {
              callback(new Error('请再次输入密码'));
          } else if (this.password1 !== this.postForm.password) {
              callback(new Error('两次输入密码不一致!'));
          } else {
              callback();
          }    
    }
    return {
      loading: false,
      postForm: {},
      fileList: [],
      fileList_enroll:[],
      listQuery: {},
      arr1:[],
      userInfo: {},
      labelWidth: '120px',
      categoryList:[],
      date_of_birth:'',
      baoming:'',
      listbaoming:[],
      password1:'',
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        password1: [{ required: true, trigger: 'blur', validator: validatePassword1 },]
      },
    }
  },
  computed: {
    ...mapGetters([
      'roles'
    ])
  },
  created() {
    //获取用户信息
    //this.$router.push('/enroll/list')
      const fileName = this.$route.params.fileName
      getOneUser(fileName).then(response => {
        console.log(response)
        this.postForm=response.data
        this.postForm.password=''
      }
   ).catch(() => {
        this.loading = false
      })
      //console.log(fileName)
  },
  methods: {
    onContentClick(data) {
      console.log(data)
      if (data.text) {
        window.open(data.text)
      }
    },
    setDefault() {
      // this.postForm = Object.assign({}, defaultForm)
      this.fileList = []
      this.$router.push({
        path: '/user/list',
      })
      //this.postForm={state:"1"}
    },
    handlePreview(file){

    },

    handleRemove(file, fileList) {
      this.setDefault()
    },
    getCategoryList() {
      
    },
    submitForm() {
      const onSuccess = (response) => {
        const { msg } = response
        this.$notify({
          title: '操作成功',
          message: msg,
          type: 'success',
          duration: 2000
        })
        this.loading = false
      }

      if (!this.loading) {
        this.loading = true
        this.$refs.postForm.validate((valid, fields) => {
          console.log(valid, fields)
          if (valid) {
            const enroll = Object.assign({}, this.postForm)
            updatePasswordUser(enroll).then(response => {
                onSuccess(response)
                this.setDefault()
              }).catch(() => {
                this.loading = false
              })
          } else {
            const message = fields[Object.keys(fields)[0]][0].message
            this.$message({ message, type: 'error' })
            this.loading = false
          }
        })
      }
    },
    showGuide() {
      console.log('show guide...')
    }
  }
}
</script>

<style lang="scss" scoped>
  .detail-container {
    padding: 40px 50px 20px;
    .preview-img {
      width: 200px;
      height: 270px;
    }
  }
</style>
